package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.Alignment
import androidx.compose.material3.Icon
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.R
import com.promise.jokerdream.database.entity.FactionCommerceGameConfigEntity
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.CheckboxOptionRow
import com.promise.jokerdream.ui.components.MultiSelectHashtagBottomSheet
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.model.faction.FactionCommerceRewardConfig

/**
 * 商会设置卡片
 */
@Composable
fun FactionCommerceSettingCard(
    config: FactionCommerceGameConfigEntity,
    onChamberOfCommerceChange: (Boolean) -> Unit,
    onTreasureBoxChange: (Boolean) -> Unit,
    onTradingChange: (Boolean) -> Unit,
    onTradingRewardsChange: (List<String>) -> Unit,
    onExchangeChange: (Boolean) -> Unit,
    onExchangeRewardsChange: (List<String>) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    var showTradingBottomSheet by remember { mutableStateOf(false) }
    var showExchangeBottomSheet by remember { mutableStateOf(false) }
    
    SettingCard(
        title = "商会",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onChamberOfCommerceChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                // 转换 selectedTradingRewards: String -> List<String>
                val selectedTradingRewards = if (config.selectedTradingRewards.isEmpty()) {
                    emptyList()
                } else {
                    config.selectedTradingRewards.split(",").map { it.trim() }
                }
                // 转换 selectedExchangeRewards: String -> List<String>
                val selectedExchangeRewards = if (config.selectedExchangeRewards.isEmpty()) {
                    emptyList()
                } else {
                    config.selectedExchangeRewards.split(",").map { it.trim() }
                }
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // 商会宝箱功能
                    CheckboxOptionRow(
                        title = "商会宝箱",
                        checked = config.treasureBoxEnabled,
                        onCheckedChange = onTreasureBoxChange
                    )
                    
                    // 商会交易功能
                    CheckboxOptionRow(
                        title = "商会交易",
                        checked = config.tradingEnabled,
                        onCheckedChange = onTradingChange
                    )
                    
                    // 商会交易奖励选择
                    if (config.tradingEnabled) {
                        SelectionRow(
                            label = "选择奖励：",
                            selectedText = if (selectedTradingRewards.isEmpty()) "未选择" else "已选择${selectedTradingRewards.size}项",
                            onClick = { showTradingBottomSheet = true }
                        )
                    }
                    
                    // 商会兑换功能
                    CheckboxOptionRow(
                        title = "商会兑换",
                        checked = config.exchangeEnabled,
                        onCheckedChange = onExchangeChange
                    )
                    
                    // 商会兑换奖励选择
                    if (config.exchangeEnabled) {
                        SelectionRow(
                            label = "选择奖励：",
                            selectedText = if (selectedExchangeRewards.isEmpty()) "未选择" else "已选择${selectedExchangeRewards.size}项",
                            onClick = { showExchangeBottomSheet = true }
                        )
                    }
                }
            }
        }
    )

    // 底部弹窗
    if (showTradingBottomSheet) {
        // 转换 selectedTradingRewards: String -> List<String>
        val selectedTradingRewards = if (config.selectedTradingRewards.isEmpty()) emptyList() else config.selectedTradingRewards.split(",").map { it.trim() }
        MultiSelectHashtagBottomSheet(
            title = "选择交易奖励",
            initialTags = selectedTradingRewards,
            placeholder = "输入奖励名称，回车添加",
            recommendedTags = FactionCommerceRewardConfig.TRADING_REWARDS,
            onCancel = { showTradingBottomSheet = false },
            onComplete = { rewards ->
                onTradingRewardsChange(rewards)
                showTradingBottomSheet = false
            },
            onDismiss = { showTradingBottomSheet = false }
        )
    }
    
    // 底部弹窗
    if (showExchangeBottomSheet) {
        // 转换 selectedExchangeRewards: String -> List<String>
        val selectedExchangeRewards = if (config.selectedExchangeRewards.isEmpty()) emptyList() else config.selectedExchangeRewards.split(",").map { it.trim() }
        MultiSelectHashtagBottomSheet(
            title = "选择兑换奖励",
            initialTags = selectedExchangeRewards,
            placeholder = "输入奖励名称，回车添加",
            recommendedTags = FactionCommerceRewardConfig.EXCHANGE_REWARDS,
            onCancel = { showExchangeBottomSheet = false },
            onComplete = { rewards ->
                onExchangeRewardsChange(rewards)
                showExchangeBottomSheet = false
            },
            onDismiss = { showExchangeBottomSheet = false }
        )
    }
}

